$.fn.extend({
	waterfall:function  ( item ) {
		//保存this
		var $this = this;
		//获取子元素的宽度
		var itemWidth = $this.children( item ).width();
		//获取父元素的跨度
		var parentWidth = $this.width();
		//获取一行放置子元素的个数
		var itemNum = Math.floor( parentWidth / itemWidth );
		//获取间隙
		var marginWidth = ( parentWidth - itemWidth * itemNum ) / ( itemNum + 1 );
		//准备一个数组 接收高度,初始值为一个间隙高度
		var heightArr = [];
		for (var i = 0; i < itemNum ; i++) {
			heightArr[ i ] = marginWidth;
		};
		//遍历所有 找到最小的高度,然后设置给新的内容
		$this.children( item ).each(function(index, element) {
			//定义一个最小的值
			var minNum = 0;
			var minHeight = heightArr[ minNum ];
			//循环 比出最小高度
			for (var i = 0; i < heightArr.length; i++) {
				if ( heightArr[ i ] < minHeight ) {
					minHeight = heightArr[ i ];
					minNum = i;
				};
			};
			//设置样式
			$(element).css({
				top: minHeight,
				left: marginWidth + (marginWidth + itemWidth ) * minNum
			});
			var currentHeight = heightArr[ minNum ];
			currentHeight += marginWidth;
			currentHeight += $(element).height();
			heightArr[ minNum ] = currentHeight;

		});
		return $this;
	}
})